import LogFactory from "log/LogFactory";
import {SecurityRole} from "type/SecurityRole";
import {createAsyncThunk, createSlice, PayloadAction} from "@reduxjs/toolkit";
import {requestDelete, requestGet, requestPost, requestPut} from "util/request";
import {Page, PagingData} from "type/Page";
import System from "global/System";
import {StateBaseType} from "../StateBaseType";
import {ReduxStatus} from "enum/ReduxStatus";

const log = LogFactory.getLog("roleReducer") ;

export type RoleState = StateBaseType<SecurityRole> ;

const initialState: RoleState = {
    data: [] ,
    pageSize: System.getInstance().getUserConfig().rolePageSize,
    current: 1,
    total: 0,
    loading: false,
    status: ReduxStatus.INIT,
} ;

/**
 * 从服务器获取角色信息,异步调用,请求角色分页数据
 */
export const fetchPageData = createAsyncThunk(
    'role/fetchRolePageData',
    async (paging: PagingData) => {
        // log.requestDebug("异步获取用户分页数据", paging) ;
        const response = await requestGet<Page<SecurityRole>>(`/manager/security-role/page/${paging.current}/${paging.pageSize}`) ;
       log.request("异步获取角色分页数据",response) ;
        return response.data ;
    }
) ;

/**
 * 请求服务器更新角色信息
 */
export const fetchUpdateRole = createAsyncThunk(
    'role/fetchUpdateRole',
    async (role: SecurityRole) => {
        // log.requestDebug("异步获取用户分页数据", paging) ;
        const response =
            await requestPost<number>("/manager/security-role",role) ;
        return response.data ;
    }
) ;

/**
 * 请求服务器删除角色信息,根据角色ID
 */
export const fetchDeleteRole = createAsyncThunk(
    'role/fetchDeleteRoleById',
    async (roleId: string | undefined) => {
        // log.requestDebug("异步获取用户分页数据", paging) ;
        const response =
            await requestDelete<number>("/manager/security-role/" + roleId,{}) ;
        return response.data ;
    }
) ;
/**
 * 请求服务器添加角色信息
 */
export const fetchAddRole = createAsyncThunk(
    'role/fetchAddRole',
    async (role: SecurityRole) => {
        const response =
            await requestPut<number>("/manager/security-role",role) ;
        return response.data ;
    }
) ;

export const roleSlice = createSlice({
    name: 'role',
    initialState,
    reducers: {
        deleteRoleById: (state, action: PayloadAction<number>) => {
            log.debug("根据角色Id在redux中删除用户信息" , action.payload) ;
        },
    },
    extraReducers: builder => {
        builder.addCase(fetchPageData.pending,(state, action) => {
            // log.debug("从服务器读取角色分页数据准备中");
            state.status = 1 ;
            state.loading = true ;
        }) ;
        builder.addCase(fetchPageData.fulfilled,(state, action) => {
            // log.debug("从服务器读取用户分页数据成功",action.payload);
            state.status = 3 ;
            state.loading = false ;
            state.data = action.payload.data ;
            state.total = action.payload.total ;
            state.current = action.payload.currentPage;
        }) ;
        builder.addCase(fetchPageData.rejected,(state, action) => {
            log.warn("从服务器读取用户分页数据失败");
            state.status = 2 ;
            state.loading = false ;
        }) ;

        builder.addCase(fetchUpdateRole.pending,(state, action) => {
            log.debug("从服务器更新角色信息执行中");
        }) ;
        builder.addCase(fetchUpdateRole.fulfilled,(state, action) => {
            log.debug("从服务器更新角色信息成功");
            // 更新成功后要求页面重新加载数据
            state.status = 4 ;
            state.loading = true ;
        }) ;

        builder.addCase(fetchDeleteRole.pending,(state, action) => {
            log.debug("从服务器删除角色信息执行中");
            state.loading = true ;
        }) ;
        builder.addCase(fetchDeleteRole.fulfilled,(state, action) => {
            log.debug("从服务器删除角色信息成功");
            // 删除成功后要求页面重新加载数据
            state.status = 4 ;
            state.loading = false ;
        }) ;

        builder.addCase(fetchAddRole.pending,(state, action) => {
            log.debug("从服务器添加角色信息执行中");
        }) ;
        builder.addCase(fetchAddRole.fulfilled,(state, action) => {
            log.debug("从服务器添加角色信息成功");
            // 添加成功后要求页面重新加载数据
            state.status = 4 ;
            state.loading = false ;
        }) ;

    }
});

export const { deleteRoleById } = roleSlice.actions;
export default roleSlice.reducer;